﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Lottery.Client.SL.Controls
{
    public partial class UserList : UserControl
    {
        private bool _bool1;
        private SearchUserWindow _searchUserWindow;
        private AddMember _addMember;
        private List<NavUserInfo> _navUserInfosList;
        private ObservableCollection<UserInfo> _userInfos;
        private RequestReportDataStruct _requestReportDataStruct;
        private int _int1;
        private UserInfo _userInfo1;
        private UserInfo _userInfo2;
        private UserInfo _userInfo3;
        private double _double1;
        private bool _bool2;
        private int _int2;
        private bool _bool3;
        private bool _bool4;
        private UserInfo _userInfo4;
        public UserList()
		{
			this._navUserInfosList = new List<NavUserInfo>();
			this._userInfos = new ObservableCollection<UserInfo>();
			this._int1 = 16;
			this._bool2 = true;
			this.InitializeComponent();
			this.dgList.RowHeight = (double)App.RowHeight;
			this.dgList.LoadingRow += new EventHandler<DataGridRowEventArgs>(this.dgList_LoadingRow);
			this._searchUserWindow = new SearchUserWindow();
			this._searchUserWindow.SearchUser += new SearchUserWindow.SearchUserHandler(this._searchUserWindow_SearchUser);
			this._addMember = new AddMember();
			this._addMember.Closed += new EventHandler(this.peHFhqdjvF);
			this.btRefreshList.Click+=(new RoutedEventHandler(this.Pc6Ft3DL7G));
			this.btSearchList.Click+=(new RoutedEventHandler(this.aAqFSYN9EB));
			this._requestReportDataStruct = new RequestReportDataStruct();
			this._requestReportDataStruct.rows = this._int1;
			this._requestReportDataStruct.sord = true;
			this._userInfo1 = new UserInfo();
			this._userInfo1.ID = -1;
			this._userInfo1.Name = "";
			this._userInfo2 = new UserInfo();
			this._userInfo2.ID = -1;
            this._userInfo2.Name = "";
			this.navControl.LowerClicked += new NavControl.LowerDataEventHandler(this.c36FgLWt3E);
			this.dpList.PageSize = this._int1;
			this.dpList.PageIndexChanged += new EventHandler<EventArgs>(this.ypTFubRrPA);
			this.dgList.ItemsSource = this._userInfos;
			this.btAddLower.Click+=(new RoutedEventHandler(this.aVZFUeub0L));
			this.lbWaiting.Text=("正在接收数据...");
			this.lrTFLjV634(0);
		}
        
        private void peHFhqdjvF(object obj, EventArgs eventArgs)
        {
            if (this._addMember.DialogResult.HasValue)
            {
                this.XgsF7J8eob();
            }
        }
        private void aVZFUeub0L(object obj, RoutedEventArgs routedEventArgs)
        {
            this._addMember.ShowAddWindow(DataCache.CurrentUserInfo.BackPct);
        }
        private void lrTFLjV634(int num)
        {
            if (num < 0)
            {
                num = 0;
            }
            if (this._bool2)
            {
                this._bool2 = false;
            }
            else
            {
                if (this._requestReportDataStruct.lowerId == num)
                {
                    return;
                }
            }
            this._requestReportDataStruct.lowerId = num;
            this._requestReportDataStruct.firstRequest = true;
            this._requestReportDataStruct.page = 0;
            this.Search();
        }
        private void Search()
        {
            if (this._bool2)
            {
                this._bool2 = false;
                this._requestReportDataStruct.firstRequest = true;
                this._requestReportDataStruct.page = 0;
            }
            //this.gridLoading.Visibility = Visibility.Visible;
            //App.app_client.GetLowerListCompleted += new ClientControl.RecvDataEventHandler(this.W6sF4hybun);
            //App.app_client.GetLowerList(this._requestReportDataStruct);
        }
        private void W6sF4hybun(object obj, byte[] datas)
        {
           //// App.app_client.GetLowerListCompleted -= new ClientControl.RecvDataEventHandler(this.W6sF4hybun);
           // this._bool3 = PublicFunction.CheckAgent(BcZJxKMIDcH0kvSFls.w4WekCpilj(22356));
           // this._bool4 = PublicFunction.CheckAgent(BcZJxKMIDcH0kvSFls.w4WekCpilj(22362));
           // this._requestReportDataStruct._search = false;
           // this._requestReportDataStruct.firstRequest = false;
           // this._requestReportDataStruct.searchFiled = "";
           // this._requestReportDataStruct.searchString = "";
           // this.gridLoading.Visibility = Visibility.Collapsed;
           // this._userInfos.Clear();
           // LowerListDatas lowerListDatas = LowerListDatas.GetLowerListDatas(datas);
           // if (lowerListDatas == null)
           // {
           //     return;
           // }
           // if (lowerListDatas.Rows != null && lowerListDatas.Rows.get_Count() > 0)
           // {
           //     this._int2 = lowerListDatas.Page * this._int1 + 1;
           //     this._userInfo2.Balance = (this._userInfo2.Freeze = 0.0);
           //     this._userInfo3 = lowerListDatas.Rows[0);
           //     this._double1 = ((this._userInfo3.CommenderID == 1) ? this._userInfo3.BackPct : ((this._userInfo3.BackPct > 0.045) ? (this._userInfo3.BackPct - 0.001) : (this._userInfo3.BackPct - 0.005)));
           //     using (List<UserInfo>.Enumerator enumerator = lowerListDatas.Rows.GetEnumerator())
           //     {
           //         while (enumerator.MoveNext())
           //         {
           //             UserInfo current = enumerator.get_Current();
           //             this._userInfo2.Balance += current.Balance;
           //             this._userInfo2.Freeze += current.Freeze;
           //             this._userInfos.Add(current);
           //         }
           //     }
           //     this._userInfos.Add(this._userInfo2);
           //     if (lowerListDatas.Records <= this._int1)
           //     {
           //         this._userInfo1.Balance = this._userInfo2.Balance;
           //         this._userInfo1.Freeze = this._userInfo2.Freeze;
           //     }
           // }
           // if (lowerListDatas.NavDatas != null && lowerListDatas.NavDatas.get_Count() > 0)
           // {
           //     this._navUserInfosList = lowerListDatas.NavDatas;
           //     this.navControl.SetNav(this._navUserInfosList);
           // }
           // if (this._requestReportDataStruct.lowerId != lowerListDatas.LowerId)
           // {
           //     this._requestReportDataStruct.lowerId = lowerListDatas.LowerId;
           // }
           // if (this._requestReportDataStruct.lowerId == DataCache.CurrentUserInfo.ID)
           // {
           //     this._navUserInfosList = null;
           //     this.navControl.SetNav(this._navUserInfosList);
           // }
           // this._userInfos.Add(this._userInfo1);
           // this.MfrFCVuymt(lowerListDatas.Records, lowerListDatas.Page);
        }
        private void MfrFCVuymt(int num, int pageIndex)
        {
            List<int> list = new List<int>(num);
            for (int i = 0; i < num; i++)
            {
                list.Add(i);
            }
            PagedCollectionView pagedCollectionView = new PagedCollectionView(list);
            pagedCollectionView.PageSize = this._int1;
            pagedCollectionView.MoveToPage(pageIndex);
            this.dpList.Source = pagedCollectionView;
        }
        private void aAqFSYN9EB(object obj, RoutedEventArgs routedEventArgs)
        {
            this._searchUserWindow.tbUserName.Text=("");
            this._searchUserWindow.Show();
        }
        private void XgsF7J8eob()
        {
            this._requestReportDataStruct.firstRequest = true;
            this._requestReportDataStruct.page = 0;
            this._requestReportDataStruct._search = false;
            this._requestReportDataStruct.searchFiled = "";
            this._requestReportDataStruct.searchString = "";
            this.Search();
        }
        private void Pc6Ft3DL7G(object obj, RoutedEventArgs routedEventArgs)
        {
            this.XgsF7J8eob();
        }
        private void c36FgLWt3E(object obj, NavUserInfo navUserInfo)
        {
            this.lrTFLjV634(navUserInfo.UserId);
        }
        private void ypTFubRrPA(object obj, EventArgs eventArgs)
        {
            this.aaHFqre15D(this.dpList.PageIndex);
        }
        private void aaHFqre15D(int num)
        {
            if (num == this._requestReportDataStruct.page)
            {
                return;
            }
            this._requestReportDataStruct.page = num;
            this._requestReportDataStruct.firstRequest = false;
            this.Search();
        }
        private void dgList_LoadingRow(object obj, DataGridRowEventArgs dataGridRowEventArgs)
        {
            UserInfo userInfo = dataGridRowEventArgs.Row.DataContext as UserInfo;
            if (userInfo.ID > 0)
            {
                (this.dgList.Columns[0].GetCellContent(dataGridRowEventArgs.Row) as TextBlock).Text=(this._int2++.ToString());
                this.dgList.Columns[2].GetCellContent(dataGridRowEventArgs.Row).Visibility = Visibility.Visible;
                FrameworkElement cellContent = this.dgList.Columns[3].GetCellContent(dataGridRowEventArgs.Row);
                cellContent.Visibility = Visibility.Visible;
                (cellContent.FindName("imgArrowUp") as Image).Visibility =
                    (Math.Min(this._double1, userInfo.MaxBackPct)) > userInfo.BackPct
                        ? Visibility.Visible
                        : Visibility.Collapsed;
                this.dgList.Columns[7].GetCellContent(dataGridRowEventArgs.Row).Visibility = Visibility.Visible;
                this.dgList.Columns[8].GetCellContent(dataGridRowEventArgs.Row).Visibility = Visibility.Visible;
                FrameworkElement cellContent2 = this.dgList.Columns[9].GetCellContent(dataGridRowEventArgs.Row);
                cellContent2.Visibility = Visibility.Visible;
                (cellContent2.FindName("imgArrowUp") as Button).Visibility
                    = (userInfo.CommenderID == DataCache.CurrentUserInfo.ID) ? Visibility.Visible : Visibility.Collapsed;
                Button button = cellContent2.FindName("btChange") as Button;
                if (userInfo.CommenderID != DataCache.CurrentUserInfo.ID)
                {
                    button.Visibility = Visibility.Collapsed;
                    return;
                }
                button.Content = "??";
                if (!userInfo.IsCanLogin)
                {
                    if (this._bool4)
                    {
                        button.Content = "??";
                        button.Visibility = Visibility.Visible;
                        return;
                    }
                }
                else
                {
                    if (this._bool3)
                    {
                        button.Visibility = Visibility.Visible;
                        return;
                    }
                }
            }
            else
            {
                (this.dgList.Columns[0].GetCellContent(dataGridRowEventArgs.Row) as TextBlock).Text=("");
                this.dgList.Columns[2].GetCellContent(dataGridRowEventArgs.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[3].GetCellContent(dataGridRowEventArgs.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[7].GetCellContent(dataGridRowEventArgs.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[8].GetCellContent(dataGridRowEventArgs.Row).Visibility = Visibility.Collapsed;
                this.dgList.Columns[9].GetCellContent(dataGridRowEventArgs.Row).Visibility = Visibility.Collapsed;
            }
        }
        private void _searchUserWindow_SearchUser(object obj, string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }
            this._requestReportDataStruct._search = true;
            this._requestReportDataStruct.firstRequest = true;
            this._requestReportDataStruct.searchFiled = "";
            this._requestReportDataStruct.searchString = text;
            this.Search();
        }
        private void LowerPlayListUserName_Click(object sender, MouseButtonEventArgs e)
        {
            if (sender != null)
            {
                UserInfo userInfo = (sender as TextBlock).DataContext as UserInfo;
                if (userInfo != null && userInfo.ID >= 0 && userInfo.UserType == 1)
                {
                    this.lrTFLjV634(userInfo.ID);
                }
            }
        }
        private void EditLower_Click(object sender, RoutedEventArgs e)
        {
            if (sender != null)
            {
                UserInfo userInfo = (sender as Button).DataContext as UserInfo;
                if (userInfo.CommenderID == DataCache.CurrentUserInfo.ID)
                {
                    this._addMember.ShowEditWindow(userInfo.ID, (this._userInfo3 == userInfo) ? DataCache.CurrentUserInfo.BackPct : this._userInfo3.BackPct, userInfo.UserType, userInfo.Name, userInfo.NickName, userInfo.BackPct, userInfo.MaxBackPct);
                }
            }
        }
        private void LockLower_Click(object sender, RoutedEventArgs e)
        {
            if (sender != null && MessageBox.Show("确定要修改此账户状态吗?", "系统信息",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                return;
                this._userInfo4 = ((sender as Button).DataContext as UserInfo);
                this.gridLoading.Visibility = Visibility.Visible;
                //App.app_client.LockLowerCompleted += new ClientControl.RecvCodeEventHandler(this.qE9FG03pr2);
                //App.app_client.LockLower(this._userInfo4.ID);
            }
        }
        private void qE9FG03pr2(object obj, string text)
        {
            //App.app_client.LockLowerCompleted -= new ClientControl.RecvCodeEventHandler(this.qE9FG03pr2);
            //this.gridLoading.Visibility = Visibility.Collapsed;
            //if (!string.IsNullOrEmpty(text))
            //{
            //    App.ShowMsg(text);
            //}
            //else
            //{
            //    App.ShowMsg(MsgInfo.pub_s_999);
            //}
            //this._userInfo4 = null;
            //this.lrTFLjV634(0);
        }
    }
}
